<?
/**
 * @description Liberty Reserve Mass Payment Tool.
 * @author Carey Dayrit <carey.dayrit.org>
 *
 * $Id: index.php 12 2010-03-09 16:20:43Z carey.dayrit@gmail.com $
 * $Rev: 12 $
 * $LastChangedBy: carey.dayrit@gmail.com $
 * $LastChangedDate: 2010-03-09 16:20:43 +0000 (Tue, 09 Mar 2010) $
 *
 */


require("functions.php");


$action = $_POST[action];

$canDisplayForm = true;

$wasError = false;

$payerAcct = "";
$payerAcctError = "";

$securityWord = "";
$securityWordError = "";

$transferList = "";
$transferListError = "";

$apiName = "";
$apiNameError = "";


if ($action == "pay") {
	$payerAcct = $_POST[payerAcct];
	$securityWord = stripslashes($_POST[secWord]);
	$transferList = stripslashes($_POST[transferList]);
	$apiName = stripslashes($_POST[apiName]);

	$canDisplayForm = false;
	$canDisplayResult = true;

	if (is_null($payerAcct) || trim($payerAcct) == "") {
		$wasError = true;
		$payerAcctError = "Payer account number can't be empty.";
	}
	else if (!isValidAccountNumber($payerAcct)) {
		$wasError = true;
		$payerAcctError = "Invalid payer account number format.";
	}

	if (is_null($securityWord) || trim($securityWord) == '') {
		$wasError = true;
		$securityWordError = "Security Word can't be empty.";
	}

	if (is_null($apiName) || trim($apiName) == '') {
		$wasError = true;
		$apiNameError = "Api Name can't be empty.";
	}


	
	$request = new TransferRequest($apiName, $securityWord);
	$request->addTransfersFromText($payerAcct, $transferList, $transferListError);	
	
	$transferListError = str_replace("\n","<br />", $transferListError);
	if ($transferListError != "") {
		$wasError = true;
	}
	
}

if ($canDisplayForm || $wasError) {

	showHeader();

?>			
      <form method="post">
      <input type="hidden" name="action" value="pay"/>
      <table cellpadding="0" cellspacing="0" border="0" class="form">
        <tr>
          <td class="field-name">
            Account Number: 
          </td>
          <td class="field-value">
            <input type="text" name="payerAcct" value="<?=htmlspecialchars($payerAcct)?>"/>
						<? if ($payerAcctError != "") { ?>
						<div class="error"><?=$payerAcctError?></div>
						<? } ?>
          </td>
          <td class="field-name">
            Api Name: 
          </td>
          <td class="field-value">
            <input type="text" name="apiName" value="<?=htmlspecialchars($apiName)?>"/>
						<? if ($apiNameError != "") { ?>						
						<div class="error"><?=$apiNameError?></div>
						<? } ?>						
          </td>
        </tr>
			  <tr>
          <td class="field-name">
            Security Word: 
          </td>
          <td class="field-value">
            <input type="password" name="secWord" />
						<? if ($securityWordError != "") { ?>						
						<div class="error"><?=$securityWordError?></div>
						<? } ?>						
          </td>
        </tr>
			</table>
			<div>
			Transfer List:
			</div>
	
			<? if ($transferListError != "") { ?>
			<div class="error"><?=$transferListError?></div>
			<? } ?>			
			
			<textarea name="transferList" style="width: 100%; height: 200px"><?=htmlspecialchars($transferList)?></textarea>
	
			<div style="padding: 15px 0 15px 0; ">
				<input type="submit" value="Pay!" style="width: 10em"/>&nbsp;&nbsp;
				<input type="reset" value="Clear Form" style="width: 10em"/>
			</div>
      </form>
<? 

	showFooter();
} 
else if ($canDisplayResult) {

	showHeader();


	
?>					

<!--			
<?
	$responseContent = $request->getResponse();
?>
-->

	<p> 
<?
	$responseParseErrors = "";
	$responseObjectsCount = 0;
	if (trim($responseContent) != "") {
		$responseObjects = $request->parseResponse($responseContent, $responseParseErrors);
		$responseObjectsCount = count($responseObjects);
	}
	else {
		$responseParseErrors = "Server returned an empty response.";
	}
?>

	</p>


	<p>
		Payer Account: <strong><?=$payerAcct?></strong>
	</p>
<?

	if ($responseParseErrors != "") {
?>	
		<div class="error">
			<b>Could not pay via API. Intercommunication error.</b><br />
			Check your Liberty Reserve transaction history to ensure 
			that no transfers were made before trying to repeat the same transfers.<br />
			<b>Details:</b><br />
			<?=$responseParseErrors?>
		</div>
<?		
	}
	if ($responseObjectsCount > 0) {

?>	
			
			<table class="transfers" cellspacing="0" width="100%">
				<thead>
				<tr>
					<th>
						
					</th>
					<th>
						Payee Acct
					</th>
					<th>
						Amount
					</th>
					<th>
						Fee
					</th>
					<th>
						Closing Balance
					</th>
					<th>
						Id
					</th>
					<th>
						Memo
					</th>
				</tr>
				</thead>
				<tbody>
<?
		for ($i = 0; $i < $responseObjectsCount; $i++) {
			$ro = $responseObjects[$i];
?>					
					<tr>
						<td style="text-align: center">
							<?=(method_exists($ro, "TransferReceipt") ?  "<div class=\"success\">Success</div>" : "<div class=\"error\">Error</div>")?>
						</td>	
						<td>
							<?=$ro->transfer->payeeAcct?>
						</td>	
						<td>
							<?=$ro->transfer->amount?>
						</td>	
						<td>
							<?=(method_exists($ro, "TransferReceipt") ?  $ro->fee : "")?>
						</td>	
						<td>
							<?=(method_exists($ro, "TransferReceipt") ?  $ro->closingBalance : "")?>
						</td>	
						<td>
							<?=(method_exists($ro, "TransferReceipt") ? $ro->id : "")?>
						</td>
						<td>
							<?=$ro->transfer->memo?>
						</td>	
					</tr>
					
<?
			if (method_exists($ro, "ApiError")) {
?>					
					<tr>
						<td colspan="3" class="underline-hint">
							Info: <?=$ro->code?>: <?=$ro->text?>
						</td>
					</tr>
					
<?
			}
		}
?>
				</tbody>
			</table>	
<?		
	}
	else {
?>
		&nbsp;
<?
			
	}
	
	showFooter();
}

?>		
